package com.xs.mvc.curator;

import java.util.concurrent.TimeUnit;

import org.apache.curator.RetryPolicy;
import org.apache.curator.RetrySleeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryForever;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

/**
 * @Title: <P>WrapperZk.java</P>
 * @Package <P>com.xs.mvc.curator</P>
 * @Description: <P>WrapperZk</P>
 * @author renweiping
 * @date 2019年2月19日 下午3:04:33
 * @version 1.0
 */



@Configuration
//@PropertySource("classpath:curator.properties")
public class CuratorConfig {

	@Value("${curator.retryCount}")
    private int retryCount;
	@Value("${curator.elapsedTimeMs}")
    private int elapsedTimeMs;
	@Value("${curator.connectString}")
    private String connectString;
	@Value("${curator.sessionTimeoutMs}")
    private int sessionTimeoutMs;
	@Value("${curator.connectionTimeoutMs}")
    private int connectionTimeoutMs;
	@Value("${curator.baseSleepTimeMs}")
	private int baseSleepTimeMs;
	@Value("${curator.maxRetries}")
	private int maxRetries;
	
	

	public int getRetryCount() {
		return retryCount;
	}

	public void setRetryCount(int retryCount) {
		this.retryCount = retryCount;
	}

	public int getElapsedTimeMs() {
		return elapsedTimeMs;
	}

	public void setElapsedTimeMs(int elapsedTimeMs) {
		this.elapsedTimeMs = elapsedTimeMs;
	}

	public String getConnectString() {
		return connectString;
	}

	public void setConnectString(String connectString) {
		this.connectString = connectString;
	}

	public int getSessionTimeoutMs() {
		return sessionTimeoutMs;
	}

	public void setSessionTimeoutMs(int sessionTimeoutMs) {
		this.sessionTimeoutMs = sessionTimeoutMs;
	}

	public int getConnectionTimeoutMs() {
		return connectionTimeoutMs;
	}

	public void setConnectionTimeoutMs(int connectionTimeoutMs) {
		this.connectionTimeoutMs = connectionTimeoutMs;
	}
    
	@Bean 
	public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { 
		return new PropertySourcesPlaceholderConfigurer(); 
	}

	@Override
	public String toString() {
		return "WrapperZk [retryCount=" + retryCount + ", elapsedTimeMs=" + elapsedTimeMs + ", connectString="
				+ connectString + ", sessionTimeoutMs=" + sessionTimeoutMs + ", connectionTimeoutMs="
				+ connectionTimeoutMs + "]";
	} 
	
	@Bean
	public CuratorFramework newClient(){
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client =
				CuratorFrameworkFactory.newClient(
				                        this.connectString,
				                        this.sessionTimeoutMs,
				                        this.connectionTimeoutMs,
				                        retryPolicy);
		client.start();
		return client;
	}
    
}  
